# _*_ coding: utf-8 _*_
# @Time : 2020/10/19 15:25
# @Author : liupengfei
# @File : serializers.py

from rest_framework import serializers
from apps.member.stores.models import Store
from apps.system.serializers import UploadSerializer
from .bargain.serializers import BargainActivityDetailSerializer
from .models import Activity, MemberActivity, CollectActivity
from .vote.serializers import VoteActivityDetailSerializer


class ActivityListSerializers(serializers.ModelSerializer):
    activity_type_text = serializers.CharField(source='get_activity_type_display', read_only=True)
    images = serializers.SerializerMethodField()
    merchant = serializers.SerializerMethodField()
    detail = serializers.SerializerMethodField()
    status_text = serializers.CharField(source='get_status_display', read_only=True)

    def get_detail(self, obj):
        if obj.activity_type == Activity.BARGAIN:
            bargain = obj.bargain.all().first()
            return {
                'new_price': bargain.new_price,
                'old_price': bargain.old_price,
                'remain_count': bargain.remain_count,
            }
        elif obj.activity_type == Activity.VOTE:
            return ''

    def get_merchant(self, obj):
        store = Store.objects.filter(merchant=obj.merchant).first()
        ret = {
            'store_logo': store.thumbnail.get_url() if store.thumbnail else '',
            'store_name': store.name
        }
        return ret

    def get_images(self, obj):
        return UploadSerializer(obj.images, many=True).data

    class Meta:
        model = Activity
        fields = (
            'id', 'activity_type', 'brown_count', 'activity_type_text', 'title', 'brown_count', 'merchant', 'images',
            'detail', 'status_text')


class ActivityDetailSerializers(serializers.ModelSerializer):
    images = serializers.SerializerMethodField()
    activity_type_text = serializers.CharField(source='get_activity_type_display', read_only=True)
    vote = serializers.SerializerMethodField()
    bargain = serializers.SerializerMethodField()
    apply_info = serializers.SerializerMethodField()
    is_collect = serializers.SerializerMethodField()

    def get_is_collect(self, obj):
        row = CollectActivity.objects.filter(member=self.context['request'].user.member, activity=obj)
        if row:
            return True
        return False

    def get_apply_info(self, obj):
        # 是否报名
        ret_msg = {
            'is_apply': False
        }
        member_activity = MemberActivity.objects.filter(activity=obj,
                                                        member=self.context['request'].user.member).first()
        if member_activity:
            ret_msg['is_apply'] = True
            if obj.activity_type == Activity.VOTE:
                activity_vote_apply = member_activity.member_vote_apply.all().first()
                if activity_vote_apply:
                    ret_msg['number'] = activity_vote_apply.number
            if obj.activity_type == Activity.BARGAIN:
                member_bargain_apply = member_activity.member_bargain_apply.all().first()
                if member_bargain_apply:
                    ret_msg['bargain_apply_id'] = member_bargain_apply.id

        return ret_msg

    def get_vote(self, obj):
        if obj.activity_type == Activity.VOTE:
            vote = obj.vote.all().first()
            return VoteActivityDetailSerializer(vote).data
        return ''

    def get_bargain(self, obj):
        if obj.activity_type == Activity.BARGAIN:
            bargain = obj.bargain.all().first()
            return BargainActivityDetailSerializer(bargain).data
        return ''

    def get_images(self, obj):
        return UploadSerializer(obj.images, many=True).data

    class Meta:
        model = Activity
        fields = ('id', 'activity_type', 'activity_type_text', 'title', 'start_time', 'end_time', 'rules', 'content',
                  'images', 'vote', 'bargain', 'apply_info', 'brown_count', 'is_collect')


class MemberActivitySerializers(serializers.ModelSerializer):
    status_text = serializers.CharField(source='get_status_display', read_only=True)
    activity = serializers.SerializerMethodField()

    def get_activity(self, obj):
        return ActivityListSerializers(obj.activity, context={'request': self.context['request']}).data

    class Meta:
        model = MemberActivity
        fields = '__all__'


class ActivityCudSerializers(serializers.ModelSerializer):
    class Meta:
        model = Activity
        fields = '__all__'
